package com.bankapp.domain.bank;

import com.bankapp.exception.bank.BankException;
import java.io.Serializable;

public abstract class AbstractAccount implements Account, Serializable {

    private double balance;

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public String toString(){
        String simpleName = getClass().getSimpleName();

        return new StringBuffer()
                .append(simpleName)
                .append("{")
                .append("balance=").append(balance)
                .append("}")
                .toString();
    }

    @Override
    public double maximumAmountToWithDraw() {
        return balance;
    }

    @Override
    public long decimalValue() {
        return Math.round(balance);
    }

    @Override
    public void deposit(final double amount) throws BankException {
        if (amount < 0) {
            throw new BankException("Negative amount to deposit!");
        }
        this.setBalance(this.getBalance() + amount);
    }

    @Override
    public void withdraw(final double amount) throws BankException {
        if (amount < 0) {
            throw new BankException("Negative amount to withdraw");
        }
        if (this.getBalance() >= amount) {
            this.setBalance(this.getBalance() - amount);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AbstractAccount)) return false;

        AbstractAccount account = (AbstractAccount) o;

        if (Double.compare(account.balance, balance) != 0) return false;

        return true;
    }

    @Override
    public int hashCode() {
        long temp = Double.doubleToLongBits(balance);
        return (int) (temp ^ (temp >>> 32));
    }
}
